package com.yihuier.yihuier.Controller.Common;

import com.yihuier.yihuier.Service.Common.FileService;
import com.yihuier.yihuier.Util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.*;
import java.util.Map;

@RestController
@RequestMapping("/Common/download/")
public class DownloadController {

    @Autowired
    private FileService fileService;

    @GetMapping("getFile.file")
    public ResponseMessage downloadFile(@PathParam("data_id") int data_id, HttpServletRequest request,HttpServletResponse response) {
        Map<String, String> downloadFile = fileService.getDataAddressAndName(data_id);//被下载的文件在服务器中的绝对路径
        if (downloadFile.isEmpty()) {
            return new ResponseMessage(ResponseMessage.ERROR, "文件已被删除");
        }
//        String Suffix = FileController.getFileUrlSuffix(downloadFile.get("data_address"));
//        String fileName = downloadFile.get("data_name") + Suffix;//被下载文件的名称
        String fileName = downloadFile.get("data_name");//被下载文件的名称
        File file = new File(downloadFile.get("data_address"));
        if (file.exists()) {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream outputStream = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    outputStream.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                return new ResponseMessage(ResponseMessage.SUCCESS, "下载成功");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return new ResponseMessage(ResponseMessage.ERROR, "文件下载失败");
    }

    @PostMapping("getFileForAndroid.file")
    public ResponseMessage getFileForAndroid(@PathParam("data_id") int data_id){
        String filePath = fileService.getFileUrlForAndroid(data_id);
        return new ResponseMessage(ResponseMessage.ERROR, "文件路径获取成功",FileController.filePathDispose(filePath));
    }
}
